<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%@ page import="com.beaut.project.util.StaticData" %>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String inventory_id = request.getParameter("inventory_id") == null ? "" : request.getParameter("inventory_id");
	String warehouse_id = request.getParameter("warehouse_id") == null ? "" : request.getParameter("warehouse_id");
	String type = request.getParameter("type") == null ? "" : request.getParameter("type");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(inventory_id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建盘点单":"编辑盘点单" %></title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>

		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		var flag="<%=flag%>";
		var type="<%=type%>";
		var warehouse_id="<%=warehouse_id%>";
		$(document).ready(function(){
			$("#mSearchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 domSearch();
		       	} 
		   	}); 
		
		if(type==0&&'<%=flag%>' != 1){
			$("#stb").hide();
			$("#xmtb").hide();
			$("#gdtb").hide();
			$("#cSearchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		docSearch2();
		       	} 
		   	}); 
			$("#ctype_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=1',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					docSearch2();
				}
			});
			$("#customer_id").combogrid({
				idField:"id",
				textField:"company",
				title:'选择客户',
				url: '<%=contextPath %>/springMVC/crm/listPageCustomer',
			    panelWidth:550,
			    panelHeight:400,
			    rownumbers:true,
			    nowrap:false,
			    multiple:true,
				fitColumns:true,
				frozenColumns:[[
								{field:'ck',checkbox:true},
							    {title:'主键',field:'id',hidden:true}
							]],
			    columns: [[  
			       {title:'客户类别',field:'type_name',width:200,align:'center'},
				   {title:'公司名称',field:'company',width:200,align:'center'},
				   {title:'联系人',field:'contact',width:200,align:'center'},
				   {title:'TEL',field:'tel',width:200,align:'center'}
			    ]],
			    toolbar:'#ctb',
			    pageSize:10,
			    pageList : [10,50,100],  
				pagination:true,
				onChange:function(){
					var id=$('#warehouse_id').combobox('getValues').join(',');
					if(id!="" &&id != null){
						var inventory_obj=$('#customer_id').combobox('getValues').join(',');
						$('#table').datagrid('load',{
									warehouse_id:id,
									inventory_obj:inventory_obj,
									type:type
								}
							);
						}
				}
			});
		}else if(type==4&&'<%=flag%>' != 1){
		$("#ctb").hide();
		$("#xmtb").hide();
		$("#gdtb").hide();
		$("#sSearchValue").keypress(function(e) { 
	    	// 回车键事件 
	     	if(e.which == 13) { 
	     		docSearch3();
	       	} 
	   	}); 
		$("#stype_id").combotree({
			url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=2',
			method:'POST',
			animate:true,
			lines:true,
			onChange:function(){
				docSearch3();
			}
		});
		$("#supplier_id").combogrid({
			idField:"id",
			textField:"company",
			title:'选择厂家',
			url: '/project/springMVC/crm/listPageSupplier',
		    panelWidth:550,
		    panelHeight:400,
		    rownumbers:true,
		    fitColumns:true,
		    nowrap:false,
		    multiple:true,
			frozenColumns:[[
							{field:'ck',checkbox:true},
						    {title:'主键',field:'id',hidden:true}
						]],
		    columns: [[  
		       {title:'厂家类别',field:'type_name',width:200,align:'center'},
			   {title:'厂家名称',field:'company',width:200,align:'center'},
			   {title:'联系人',field:'contact',width:200,align:'center'},
			   {title:'TEL',field:'tel',width:200,align:'center'}
		    ]],
		    toolbar:'#stb',
		    pageSize:10,
		    pageList : [10,50,100],  
			pagination:true,
			onChange:function(){
				var id=$('#warehouse_id').combobox('getValues').join(',');
				if(id!="" &&id != null){
					var inventory_obj=$('#supplier_id').combobox('getValues').join(',');
					$('#table').datagrid('load',{
								warehouse_id:id,
								inventory_obj:inventory_obj,
								type:type
							}
						);
					}
			}
		});
	}else if(type==3&&'<%=flag%>' != 1){
		$("#ctb").hide();
		$("#stb").hide();
		$("#xmtb").hide();
		$("#gdSearchValue").keypress(function(e) { 
	    	// 回车键事件 
	     	if(e.which == 13) { 
	     		 doSearchGd();
	       	} 
		});
		$("#gdetp_id").combotree({
			url:'<%=contextPath %>/springMVC/group/listDepartment',
			method:'POST',
			animate:true,
			lines:true,
			onChange:function(){
				doSearchGd();
			}
		});
		$("#gd_id").combogrid({
			idField:"id",
			textField:"gd_name",
			title:'选择工单',
			url: '/project/springMVC/gd/listPageWarehouseGD',
		    panelWidth:550,
		    panelHeight:400,
		    rownumbers:true,
		    fitColumns:true,
		    multiple:true,
			frozenColumns:[[
							{field:'ck',checkbox:true},
						    {title:'主键',field:'id',hidden:true}
						]],
		    columns: [[  
					{title:'派单人',field:'gd_admin_name',width:80,align:'center'},
					{title:'所属部门',field:'dept_name',width:100,align:'center'},
					{title:'工单编号',field:'gd_code',width:100,align:'center'},
					{title:'工单名称',field:'gd_name',width:100,align:'center'},
					{title:'完工日期',field:'gd_actual_end',width:100,align:'center'},
		    ]],
		    toolbar:'#gdtb',
		    pageSize:10,
		    pageList : [10,50,100],  
			pagination:true,
			onChange:function(){
				var id=$('#warehouse_id').combobox('getValues').join(',');
				if(id!="" &&id != null){
					var inventory_obj=$('#gd_id').combobox('getValues').join(',');
					$('#table').datagrid('load',{
								warehouse_id:id,
								inventory_obj:inventory_obj,
								type:type
							}
						);
					}
			}
		});
	}else if(type==1&&'<%=flag%>' != 1){
		$("#ctb").hide();
		$("#stb").hide();
		$("#gdtb").hide();
		$("#xmSearchValue").keypress(function(e) { 
	    	// 回车键事件 
	     	if(e.which == 13) { 
	     		 doSearchXm();
	       	} 
		});
		$("#xdetp_id").combotree({
			url:'<%=contextPath %>/springMVC/group/listDepartment',
			method:'POST',
			animate:true,
			lines:true,
			onChange:function(){
				doSearchXm();
			}
		});
		$("#xm_id").combogrid({
			idField:"id",
			textField:"xm_name",
			title:'选择客户',
			url: '<%=contextPath %>/springMVC/xm/listPageWarehouseXM',
		    panelWidth:550,
		    panelHeight:400,
		    rownumbers:true,
		    multiple:true,
			fitColumns:true,
			frozenColumns:[[
							{field:'ck',checkbox:true},
						    {title:'主键',field:'id',hidden:true}
						]],
		    columns: [[  
				{title:'立项人',field:'admin_name',width:100,align:'center'},
				{title:'所属部门',field:'admin_dept',width:100,align:'center'},
		        {title:'项目编号',field:'xm_code',width:200,align:'center'},
			    {title:'项目名称',field:'xm_name',width:200,align:'center'}
			  
		    ]],
		    toolbar:'#xmtb',
		    pageSize:10,
		    pageList : [10,50,100],  
			pagination:true,
			onChange:function(){
				var id=$('#warehouse_id').combobox('getValues').join(',');
				if(id!="" &&id != null){
					var inventory_obj=$('#xm_id').combobox('getValues').join(',');
					$('#table').datagrid('load',{
								warehouse_id:id,
								inventory_obj:inventory_obj,
								type:type
							}
						);
				}
			}
		});
	}else{
		$("#ctb").hide();
		$("#stb").hide();
		$("#xmtb").hide();
		$("#gdtb").hide();
		
	}
	var url="<%=contextPath %>/springMVC/warehouse/<%=isAdd ? "listWareHouseType"  : "listWareHouse"%>"+"?type=<%=type %>";

	$("#warehouse_id").combogrid({
				title:'选择仓库',
				panelWidth:550,
				panelHeight:400,
				url:url,
				idField:"id",
				textField:"name",
				rownumbers:true,
				multiple:true,
				fitColumns:true,
				frozenColumns:[[
								{field:'ck',checkbox:true},
							    {title:'主键',field:'id',hidden:true}
							]],
				columns: [[
					{field:'id',hidden:true},
					{field:'type',hidden:true},
					{field:'dept_name',title:'所属部门',width:100,align:'center'},
					{field:'name',title:'仓库名称',width:100,align:'center'}
				]],
				toolbar:'#tb',
				onChange:function(){
					var id=$('#warehouse_id').combobox('getValues').join(',');
					$('#warehouse_ids').val(id);
					var inventory_obj="";
					if(type==0){
						inventory_obj =	$('#customer_id').combobox('getValues').join(',');
					}else if(type==4){
						inventory_obj = $('#supplier_id').combobox('getValues').join(',');
					}else if(type==1){
						inventory_obj = $('#xm_id').combobox('getValues').join(',');
					}else if(type==3){
						inventory_obj = $('#gd_id').combobox('getValues').join(',');
					}
					console.log(inventory_obj=="");
					if(inventory_obj != ""){
						$('#table').datagrid('load',{
							warehouse_id:id,
							inventory_obj:inventory_obj,
							type:type
						});
					}else{
						$('#table').datagrid('load',{
							warehouse_id:id,
							type:type
						});
					}
				
				}
			});
			
			$('#mtb').hide();
			if('<%=flag%>' == 1){
				$('#ff').form('load','<%=contextPath %>/springMVC/warehouse/getInventoryById?inventory_id=<%=inventory_id%>');
			}else{
				$("#operation_date").datetimebox('setValue',getTodaySecond());
			}
			$('#table').datagrid({
				url:"<%=contextPath %>/springMVC/warehouse/<%=isAdd ? "listMaterialDetail"  : "listInventoryDetail?inventory_id=" + inventory_id%>",	
				rownumbers:true,
				fitColumns:true,
				nowrap:false,
				singleSelect:true,
				frozenColumns:[[
				    {title:'产品id',field:'material_id',hidden:true},
				    {title:'工单id',field:'purchase_id',hidden:true},
				    {title:'项目id',field:'project_id',hidden:true},
				    {title:'供货商id',field:'supplier_id',hidden:true},
				    {title:'客户id',field:'customer_id',hidden:true},
				    {title:'仓库类型',field:'type',hidden:true}
				]],
				columns:[[
					{title:'盘点仓库',field:'warehouse_name',width:250,align:'center'},
					<%if("0".equals(type)){%>
					{title:'客户名称',field:'customer_name',width:250,align:'center'},
					<%}%>
					<%if("4".equals(type)){%>
					{title:'供货商名称',field:'supplier_name',width:250,align:'center'},
					<%}%>
					<%if("1".equals(type)){%>
					{title:'项目编号',field:'xm_code',width:250,align:'center'},
					{title:'项目名称',field:'xm_name',width:250,align:'center'},
					<%}%>
					<%if("3".equals(type)){%>
					{title:'工单编号',field:'gd_code',width:250,align:'center'},
					{title:'工单名称',field:'gd_name',width:250,align:'center'},
					{title:'采购批次',field:'apply_code',width:250,align:'center'},
					<%}%>
					{title:'产品编号',field:'code',width:100,align:'center'},
					{title:'产品名称',field:'name',width:200,align:'center'},
					{title:'规格/型号',field:'format_type',width:140,align:'center'},
					{title:'单位',field:'unit_name',width:50,align:'center'},
					{title:'库存总数',field:'db_num',width:80,align:'center'},
					{title:'盘点数量',field:'actually_num',width:80,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}
					},
					{title:'差异数量',field:'different_num',width:80,align:'center',formatter:function(value,rowData,rowIndex){

						if(rowData.actually_num ==undefined || rowData.actually_num==""){
							return "";
						}
						return rowData.actually_num-rowData.db_num;
					}},
					{title:'备注',field:'remark',width:260,align:'center',editor:{
						type:'textarea'
					}}
				]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'actually_num'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						
					}
					}
					<%if("1".equals(flag)){%>
					,{
						id:'add',
						text:'导出',
						disabled:false,
						iconCls:'icon-excel',
						handler:function(){
							ExcelOut();
						}
					}
					<%}%>
			   ]
			});
		});
		function addRow(){
			$('#table').datagrid('endEdit', lastIndex);
			$('#table').datagrid('appendRow',{
				num:'0'
			});
			var data = $('#table').datagrid('getData');
			$('#table').datagrid('loadData',data);
			lastIndex = $('#table').datagrid('getRows').length-1;
			$('#table').datagrid('selectRow', lastIndex);
			$('#table').datagrid('beginEdit', lastIndex);
		}
		
		function onSubmit(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			if($('#ff').form('validate')){
				var products="";
				var jsondata=$("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
					if(products!=""){
						products+=";";
					}
					products+=jsonToStr(jsondata.rows[i]);
					var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
					if(jsondata.rows[i].name == null || jsondata.rows[i].name == ""){
						$.messager.alert('提示','第'+index+'行未选择产品！');
						return ;
					}else if(jsondata.rows[i].actually_num == null || jsondata.rows[i].actually_num < 0){
						$.messager.alert('提示','第'+index+'行数量填写不正确！');
						return ;
					}
				}
				var warehouse_ids = $('#warehouse_ids').val();
				if(!checkNullOREmpty(products,'产品为必填项')){return;}
				var operation_date = $("#operation_date").datetimebox('getValue');
				var remark = encodeURIComponent($('#remark').val());
				var code = encodeURIComponent($('#code').val());
				
				var param = "warehouse_id="+warehouse_ids+"&code="+code+"&flag=<%=type%>&person_id=<%=person_id%>&person=<%=person%>&operation_date="+operation_date+"&remark="+remark+"&products="+products;
				$(obj).hide();
				$.ajax({
					type:'POST',
					<%=isAdd?"url:'"+contextPath+"/springMVC/warehouse/addInventory',":"url:'"+contextPath+"/springMVC/warehouse/updateInventory?inventory_id="+inventory_id+"',"%>
					data:param,
					dataType:'json',
					success:function(rtJson){
						if(rtJson.rtState == '0'){
							eval('window.opener.<%=callback%>();');
							window.close();
						}
						else{
							$.messager.alert("提示",rtJson.rtMsrg);
						}
						$(obj).show();
					}
				});
			}
		}
		function ExcelOut(){
			var warehouse_id =$("#warehouse_ids").val();
			var inventory_code =$("#code").val();
			if(warehouse_id==null||warehouse_id==""){
				$.messager.alert("提示","请选择盘点仓库！");
				return false;
			}
			window.open('<%=contextPath %>/springMVC/excel/dbInventoryExcel?inventory_id=<%=inventory_id%>&warehouse_id='+warehouse_id+'&inventory_code='+inventory_code);
		}
		function docSearch2()
		{
			var cSearchValue = $('#cSearchValue').val();
			var ctype_id = $('#ctype_id').combotree('getValue');
			$('#customer_id').combogrid('clear');
			$('#customer_id').combogrid('grid').datagrid('load',{
				searchValue:cSearchValue,
				type_id:ctype_id
				});
		}
		function docSearch3()
		{
			var sSearchValue = $('#sSearchValue').val();
			var stype_id = $('#stype_id').combotree('getValue');
			$('#supplier_id').combogrid('clear');
			$('#supplier_id').combogrid('grid').datagrid('load',{
				searchValue:sSearchValue,
				type_id:stype_id
				});
		}
		function doSearchXm()
		{
			var xmSearchValue = $('#xmSearchValue').val();
			var dept_id = $("#xdetp_id").combotree('getValue');
			$('#xm_id').combogrid('grid').datagrid('load',{
				searchValue:xmSearchValue,
				dept_id :dept_id
				
			});
		}
		function doSearchGd()
		{
			var gdSearchValue = $('#gdSearchValue').val();
			var dept_id = $("#gdetp_id").combotree('getValue');
			$('#gd_id').combogrid('grid').datagrid('load',{
				searchValue:gdSearchValue,
				dept_id :dept_id
				
			});
		}
	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
							<% if(!isAdd){ %>
								<td align="right" class="td1" nowrap>盘点编号:</td>
								<td class="td2" nowrap colspan="5">
									<input  id="code" name="code" style="width:50%;" class="easyui-validatebox" data-options="required:true">
									
							<%} %>
							<% if(isAdd){ %>
								<td align="right" class="td1" nowrap >盘点编号:</td>
								<td class="td2" nowrap>
									<input  id="code" name="code" class="easyui-validatebox" data-options="required:true">
								<%if("0".equals(type)){%>
								<td align="right" class="td1" nowrap>选择客户 ：</td>
								<td class="td2" nowrap>
									<input  id="customer_id" name="customer_id" />
									<input type="hidden" id="customer_ids" name="customer_ids"  />
								</td>
								<%} %>
								<%if("1".equals(type)){%>
								<td align="right" class="td1" nowrap>选择项目 ：</td>
								<td class="td2" nowrap>
									<input  id="xm_id" name="xm_id" />
									<input type="hidden" id="xm_ids" name="xm_ids"  />
								</td>
								<%} %>
								<%if("3".equals(type)){%>
								<td align="right" class="td1" nowrap>选择工单 ：</td>
								<td class="td2" nowrap>
									<input  id="gd_id" name="gd_id" />
									<input type="hidden" id="gd_ids" name="gd_ids"  />
								</td>
								<%} %>
								<td align="right" class="td1" nowrap>盘点仓库：</td>
								<td class="td2" nowrap >
									<input  id="warehouse_id" name="warehouse_id"  data-options="required:true" >
									<input type="hidden" id="warehouse_ids" name="warehouse_ids"  >
								</td>
								<%if("4".equals(type)){%>
								<td align="right" class="td1" nowrap>选择厂家 ：</td>
								<td class="td2" nowrap>
									<input  id="supplier_id" name="supplier_id" />
									<input type="hidden" id="supplier_ids" name="supplier_ids"  />
								</td>
								<%} %>
							<%} %>
								
							</tr>
				<tr>
					<td align="right" class="td1" nowrap>盘点人：</td>
					<td class="td2" nowrap>
						<input  id="person" name="person" readOnly value="<%=person%>">
					</td>
					<td align="right" class="td1" nowrap>盘点日期：</td>
					<td class="td2" nowrap colspan="3" >
						<input  id="operation_date" name="operation_date"  class="easyui-datetimebox" data-options="required:true">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='5'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
				  <%if(!"1".equals(isShow)){ %>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='5'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="onSubmit(this);">提交</a>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
				<%} %>
				<tr>
					<td colspan="6"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
			<div id="mtb" style="padding:3px;height:auto">
				<div align="right">
					分类：<input  id="material_type_id" name="material_type_id" class="easyui-combotree">
					查询条件：<input id="mSearchValue" name="mSearchValue" style="width:150px" title="材料编码或名称"/>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="domSearch();"></a>
				</div>
			</div>
			<div id="ctb" style="padding:3px;height:auto;">
			<div align="right">
				分类：<input  id="ctype_id" name="ctype_id" >
				查询条件：<input id="cSearchValue" name="cSearchValue"  title="客户名称或联系人" placeholder="客户名称/联系人"/>
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="docSearch2();"></a>
			</div>
		</div>
		<div id="stb" style="padding:3px;height:auto;">
			<div align="right">
				分类：<input  id="stype_id" name="stype_id" >
				查询条件：<input id="sSearchValue" name="sSearchValue"  title="厂家名称或联系人" placeholder="厂家名称或联系人"/>
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="docSearch3();"></a>
			</div>
		</div>
		<div id="xmtb" style="padding:3px;height:auto;">
			<div align="right">
				所属部门：<input  id="xdetp_id" name="xdetp_id" >
				查询条件：<input id="xmSearchValue" name="xmSearchValue"  title="项目名称/项目编号/合同编号" placeholder="项目名称/项目编号/合同编号"/>
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="doSearchXm();"></a>
			</div>
		</div>
		<div id="gdtb" style="padding:3px;height:auto;">
			<div align="right">
				所属部门：<input  id="gdetp_id" name="gdetp_id" >
				查询条件：<input id="gdSearchValue" name="gdSearchValue"  title="工单名称/工单编号" placeholder="工单名称/工单编号"/>
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="doSearchGd();"></a>
			</div>
		</div>
	</body>
</html>